<?php
if (!defined('FosfoaSystem')){exit(1);}
import($GLOBALS['spConfig']['controller_path'] . '/general.php');
class pic extends general
{
    public function __construct()
    {
        parent::__construct();
    }
    function index()
    {
        $this->tt = "图库管理";
        $products_id = $this->spArgs("products_id", 0);
        $obj = spClass("productsModel");
        if ($products_id == 0)
        {
            $this->error(T('ID does not exist!'));
        }
        $obj->linker[0]['field'] = 'products_name';
        $conditions = array('products_id' => $products_id);
        $arr = $obj->spLinker()->find($conditions, '',
            'products_id,products_allimg,products_image');
        $allimg = unserialize($arr['products_allimg']);
        $this->allimg = $allimg;
        $this->imgname = $arr['detail']['products_name'];
        $this->img = $arr['products_image'];
        $this->products_id = $arr['products_id'];
        $this->display("admin/pic_index.html");
    }
    function del()
    {
        $products_id = $this->spArgs("products_id", 0);
        $id = $this->spArgs("id");
        $zt = $this->spArgs("zt");
        if ($products_id == 0)
        {
            $this->error(T('ID does not exist!'));
        }
        $obj = spClass("productsModel");
        $conditions = array('products_id' => $products_id);

        $arr = $obj->find($conditions, '', 'products_id,products_allimg,products_image');
        $allimg = unserialize($arr['products_allimg']);
        $delimg = 'upload/' . $allimg[$id];
        //echo $delimg;
        if (file_exists($delimg))
        {
            //echo "文件存在";
            if (!unlink($delimg))
            {
                echo ("Error deleting $delimg");
            } else
            {
                unset($allimg[$id]);
                $allimg = serialize($allimg);
                $data = array('products_allimg' => $allimg);
                if ($zt)
                {
                    $data['products_image'] = '';
                }
                //dump($data);
                $tt = $obj->update($conditions, $data);
                //echo $allimg;
                if ($tt)
                {
                    $this->error(T('Successfully  deleted!'));
                } else
                {
                    $this->error(T('Delete failed!'));
                }
                $msg="删除商品图片".$delimg;
                Savelog($msg);
            }

        } else
        {
            //echo "删除失败,文件不存在!";
            $this->error(T('Delete failed!'));
        }
    }
}